<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
</head>
<body>
    <h2>用户登录</h2>
    <label for="username">用户名：</label>
    <input type="text" id="username">
    <br>
    <label for="password">密码：</label>
    <input type="password" id="password">
    <br>
    <input type="button" value="登录" onclick="login()">
    <script src="../util.js"></script>
    <script>
        var username = document.querySelector("#username");
        var password = document.querySelector("#password");
        function login(){
            if(!username.value){
                alert("请输入账号")
                return;
            }
            if(!password.value){
                alert("请求输入密码")
                return;
            }
            //都输入正确的格式
            ajax({
                method: "POST",
                url: "../login",
                contentType: "application/json",
                body: JSON.stringify({
                    username: username.value,
                    password: password.value,
                }),
                callback: function(status, resp){
                    if(status == 200){
                        //转换响应正文(json字符串)为json对象
                        var body = JSON.parse(resp);
                        if(body.ok){//登录成功
                            location.href = "sensitive.html";
                        }else{//登录失败
                            alert("登录失败："+body.msg)
                        }
                    }else{
                        alert("状态码："+status)
                    }
                }
            });
        }
    </script>
</body>
</html>